<?php
/**
 * Created by PhpStorm.
 * User: liu85
 * Date: 2019/2/23
 * Time: 19:46
 */

namespace app\admin\business;


use app\common\helper\AdminInfoHelper;
use app\common\helper\RequestHelper;
use app\common\helper\ResponseHelper;
use app\common\helper\UserHelper;
use app\common\model\Admin;
use app\common\service\AdminService;
use think\facade\Session;

class AdminBusiness extends AdminBaseBusiness
{
    public function __construct(AdminInfoHelper $adminInfoHelper)
    {
        parent::__construct($adminInfoHelper);
    }

    /**
     * 登录
     * @param $userName
     * @param $pass
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function doLogin($userName, $pass)
    {
        $pass = UserHelper::makePassword($pass);
        $isCheck = AdminService::checkUserNameAndPass($userName, $pass);
        if (!$isCheck) {
            ResponseHelper::_failure('用户名/密码不正确');
        }
        $data['ip'] = RequestHelper::getClientIp();
        $last = Admin::get($isCheck)->save($data);
        Admin::get($isCheck)->setInc('hits', 1);
        if ($last) {
            Session::set("aid", $isCheck);
            ResponseHelper::_success('登录成功', '/admin/index');
        }
        ResponseHelper::_failure('登录失败,请稍后重试');
    }
}